package com.softcits.autotask.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

@Service

public class AutoTaskService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Autowired
    private PlatformTransactionManager transaction;
    
    @Transactional(readOnly=true)
    public void getDesiredData() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from t1");
        for(Map<String, Object> record: list) {
            System.out.println(record.get("id") + " : " + record.get("name"));
        }
    }
    
    public void insertData(String name) {
        jdbcTemplate.update("insert into t1 (name) values (?);", name);
        System.out.println("insert done.");
    }
    
    public void updateData(String before, String after) {
        jdbcTemplate.update("update t1 set name=? where name=?", after, before);
        System.out.println("update done.");
    }
    
    public void deleteData(String name) {
        jdbcTemplate.update("delete from t1 where name=?", name);
        System.out.println("delete done.");
    }
    
    
    public void saveDataTransaction(int mode) {
        
        jdbcTemplate.update("insert into t1 (name) values (?);", "name1");
        if(mode ==1 ) {
            throw new RuntimeException("Business process failed.");
        }
        jdbcTemplate.update("insert into t1 (name) values (?);", "name2");

        System.out.println("insert done.");
    }
    
    
}
